package com.eliza.vertx.library.tools

import io.vertx.core.Future
import io.vertx.core.http.HttpMethod
import io.vertx.core.http.HttpServer
import io.vertx.core.http.HttpServerResponse
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.Route
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext


val vertx = io.vertx.core.Vertx.vertx()

/**
 * Text1
 * Re-cap on Vert.x core HTTP servers
 */
fun text1() {
    val server: HttpServer = vertx.createHttpServer()

    server.requestHandler { request ->

        // This handler gets called for each request that arrives on the server
        val response: HttpServerResponse = request.response()
        response.putHeader("content-type", "text/plain")

        // Write to the response and end it
        response.end("Hello World!")
    }

    server.listen(8080)
}

/**
 * Router
 * Basic Vert.x-Web concepts
 */
fun router() {

    val server: HttpServer = vertx.createHttpServer()

    val router = Router.router(vertx)

    router.route().handler { ctx: RoutingContext ->

        // This handler will be called for every request
        val response = ctx.response()
        response.putHeader("content-type", "text/plain")

        // Write to the response and end it
        response.end("Hello World from Vert.x-Web!")
    }

    server.requestHandler(router).listen(8080)
}

/**
 *
 *Handling requests and calling the next handler
 */
fun next() {
    val server: HttpServer = vertx.createHttpServer()

    val router = Router.router(vertx)

    val route: Route = router.route("/some/path/")
    route.handler { ctx: RoutingContext ->
        val response = ctx.response()
        // val response = ctx.response() 启用分块响应，因为我们将在执行其他处理程序时添加数据。
        // 这仅需要一次且仅在多个处理程序进行输出时才需要。
        response.isChunked = true
        response.write("route1\n")

        // Call the next matching route after a 2 second delay
        ctx.vertx().setTimer(2000) { tid: Long? -> ctx.next() }
    }

    route.handler { ctx: RoutingContext ->
        val response = ctx.response()
        response.write("route2\n")

        // Call the next matching route after a 5 second delay
        ctx.vertx().setTimer(2000) { tid: Long? -> ctx.next() }
    }

    route.handler { ctx: RoutingContext ->
        val response = ctx.response()
        response.write("route3")

        // Now end the response
        ctx.response().end()
    }
    server.requestHandler(router).listen(8080)
}

//Simple responses
fun main() {
    val server: HttpServer = vertx.createHttpServer()

    val router = Router.router(vertx)

    router.apply {
        get("/some") // this handler will ensure that the response is serialized to json
            // the content type is set to "application/json"
            /**
             * 前面优先
             */
            .respond { ctx -> Future.succeededFuture(JsonObject().put("hello", "world")) }
//            .respond { ctx -> Future.succeededFuture(User("name", 12)) }
            .respond { ctx ->
                ctx
                    .response()
                    .putHeader("Content-Type", "text/plain")
                    .end("hello world!")
            }


        get("/path")
            .respond { ctx ->
                ctx.response()
                    .setChunked(true)
                    .write("Write some text...")
            }
            .respond { ctx ->
                ctx.response()
                    .setChunked(true)
                    .write("path/test")
            }

        get("/oko").respond { ctx ->
            gest(ctx)
        }

    }
    router.route(HttpMethod.POST, "/flights/:from-:to").handler { ctx ->
        val from = ctx.pathParam("from") // AMS

        val to = ctx.pathParam("to") // SFO
        println("$from : $to")
//        ctx
//            .response()
//            .putHeader("Content-Type", "text/plain")
//            .write("hello world!")

    }
    server.requestHandler(router).listen(8080)

}



/*
fun <T> respond(function: Function<RoutingContext, Future<T>>): Route {

}
*/
//ctx: RoutingContext

fun gest(ctx: RoutingContext): Future<Any> {
    return ctx.response()
        .setChunked(true)
//        .write("path/test")
        .end("hello world!") as Future<Any>
}
